{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 机器学习实战 ——代码样例\n",
    "\n",
    "# 第二十二章 基于 RFM 的 P2P 用户聚类模型\n",
    "\n",
    "\n",
    "注意：本案例使用的数据集为1000条样例，仅作方法参考。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 22.3.1 获取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>RECENT_DATE</th>\n",
       "      <th>TRANS_NUM</th>\n",
       "      <th>AVG_TRANS_AMT</th>\n",
       "      <th>TRANS_NUM_BEFORE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4356689447</td>\n",
       "      <td>20180125</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5985727751</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8221439519</td>\n",
       "      <td>20171211</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6255360379</td>\n",
       "      <td>20171226</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5985726631</td>\n",
       "      <td>20171203</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           ID  RECENT_DATE  TRANS_NUM  AVG_TRANS_AMT  TRANS_NUM_BEFORE\n",
       "0  4356689447     20180125        NaN            0.0               0.0\n",
       "1  5985727751     20180125        0.0            0.0               NaN\n",
       "2  8221439519     20171211        0.0            0.0               0.0\n",
       "3  6255360379     20171226        0.0            0.0               0.0\n",
       "4  5985726631     20171203        0.0            0.0               0.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入需要的库，并读取 csv 文件。\n",
    "\n",
    "import pandas as pd\n",
    "data = pd.read_csv('data.csv', encoding = 'utf-8') \n",
    "\n",
    "data.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 22.3.2 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>RECENT_DATE</th>\n",
       "      <th>TRANS_NUM</th>\n",
       "      <th>AVG_TRANS_AMT</th>\n",
       "      <th>TRANS_NUM_BEFORE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4356689447</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5985727751</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8221439519</td>\n",
       "      <td>20171211</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6255360379</td>\n",
       "      <td>20171226</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5985726631</td>\n",
       "      <td>20171203</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           ID  RECENT_DATE  TRANS_NUM  AVG_TRANS_AMT  TRANS_NUM_BEFORE\n",
       "0  4356689447     20180125        0.0            0.0               0.0\n",
       "1  5985727751     20180125        0.0            0.0               0.0\n",
       "2  8221439519     20171211        0.0            0.0               0.0\n",
       "3  6255360379     20171226        0.0            0.0               0.0\n",
       "4  5985726631     20171203        0.0            0.0               0.0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 过滤掉用户 ID 为空的用户投资记录。\n",
    "\n",
    "data = data[data['ID'].notnull()]\n",
    "\n",
    "# 投资金额或投资次数为空值，统一修正为 0。\n",
    "\n",
    "data = data.fillna(0)\n",
    "\n",
    "# 保存清洗后的数据。\n",
    "\n",
    "data.to_csv('clean_data.csv') \n",
    "\n",
    "data.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 22.3.3.\t计算 RFM 指标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>ID</th>\n",
       "      <th>RECENT_DATE</th>\n",
       "      <th>TRANS_NUM</th>\n",
       "      <th>AVG_TRANS_AMT</th>\n",
       "      <th>TRANS_NUM_BEFORE</th>\n",
       "      <th>RFM</th>\n",
       "      <th>USR_TYPE</th>\n",
       "      <th>DAYS</th>\n",
       "      <th>R</th>\n",
       "      <th>F</th>\n",
       "      <th>M</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>4356689447</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5985727751</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>8221439519</td>\n",
       "      <td>20171211</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>65</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>6255360379</td>\n",
       "      <td>20171226</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>50</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>5985726631</td>\n",
       "      <td>20171203</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>73</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>6255360403</td>\n",
       "      <td>20180109</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>36</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>8221440591</td>\n",
       "      <td>20171208</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>68</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>8221439459</td>\n",
       "      <td>20171208</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>000</td>\n",
       "      <td>L</td>\n",
       "      <td>68</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>4373540587</td>\n",
       "      <td>20171204</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>72</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>8221438327</td>\n",
       "      <td>20171122</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>84</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10</td>\n",
       "      <td>8221438387</td>\n",
       "      <td>20171123</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>83</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>11</td>\n",
       "      <td>6255358175</td>\n",
       "      <td>20180206</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>8</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>12</td>\n",
       "      <td>4373539491</td>\n",
       "      <td>20180210</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>13</td>\n",
       "      <td>6255359199</td>\n",
       "      <td>20180126</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>19</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>14</td>\n",
       "      <td>5137506867</td>\n",
       "      <td>20180210</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>15</td>\n",
       "      <td>5137506855</td>\n",
       "      <td>20180129</td>\n",
       "      <td>207.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>138.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>16</td>\n",
       "      <td>16.0</td>\n",
       "      <td>207.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>16</td>\n",
       "      <td>6851792819</td>\n",
       "      <td>20171125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>81</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>17</td>\n",
       "      <td>4362308131</td>\n",
       "      <td>20180131</td>\n",
       "      <td>24.0</td>\n",
       "      <td>5.302320e+05</td>\n",
       "      <td>84.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>14</td>\n",
       "      <td>14.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>5.302320e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>18</td>\n",
       "      <td>7446429015</td>\n",
       "      <td>20180210</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>19</td>\n",
       "      <td>8491076627</td>\n",
       "      <td>20171128</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.000000e+04</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>78</td>\n",
       "      <td>78.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.000000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>20</td>\n",
       "      <td>4362703403</td>\n",
       "      <td>20171205</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>71</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>21</td>\n",
       "      <td>8491075519</td>\n",
       "      <td>20180201</td>\n",
       "      <td>20.0</td>\n",
       "      <td>4.966667e+04</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>13</td>\n",
       "      <td>13.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>4.966667e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>22</td>\n",
       "      <td>8221435907</td>\n",
       "      <td>20171211</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>65</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>23</td>\n",
       "      <td>4334214987</td>\n",
       "      <td>20180130</td>\n",
       "      <td>48.0</td>\n",
       "      <td>4.800000e+04</td>\n",
       "      <td>36.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>15</td>\n",
       "      <td>15.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>4.800000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>24</td>\n",
       "      <td>4362301135</td>\n",
       "      <td>20171122</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>84</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>25</td>\n",
       "      <td>7103577783</td>\n",
       "      <td>20180115</td>\n",
       "      <td>27.0</td>\n",
       "      <td>5.400000e+04</td>\n",
       "      <td>63.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>30</td>\n",
       "      <td>30.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>5.400000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>26</td>\n",
       "      <td>6339286107</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>27</td>\n",
       "      <td>5985721899</td>\n",
       "      <td>20180120</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>6.0</td>\n",
       "      <td>000</td>\n",
       "      <td>L</td>\n",
       "      <td>25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>28</td>\n",
       "      <td>4412856783</td>\n",
       "      <td>20180123</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>22</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>29</td>\n",
       "      <td>4356682487</td>\n",
       "      <td>20180202</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>95.0</td>\n",
       "      <td>000</td>\n",
       "      <td>L</td>\n",
       "      <td>12</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>970</th>\n",
       "      <td>970</td>\n",
       "      <td>8142865064</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>971</th>\n",
       "      <td>971</td>\n",
       "      <td>4519660412</td>\n",
       "      <td>20180201</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>13</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>972</th>\n",
       "      <td>972</td>\n",
       "      <td>4339903456</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>973</th>\n",
       "      <td>973</td>\n",
       "      <td>4339903444</td>\n",
       "      <td>20180106</td>\n",
       "      <td>360.0</td>\n",
       "      <td>9.391169e+06</td>\n",
       "      <td>640.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>39</td>\n",
       "      <td>39.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>9.391169e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>974</th>\n",
       "      <td>974</td>\n",
       "      <td>8142864968</td>\n",
       "      <td>20171116</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>90</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>975</th>\n",
       "      <td>975</td>\n",
       "      <td>4356749864</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>976</th>\n",
       "      <td>976</td>\n",
       "      <td>4519653416</td>\n",
       "      <td>20180210</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>977</th>\n",
       "      <td>977</td>\n",
       "      <td>9260714012</td>\n",
       "      <td>20180202</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>12</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>978</th>\n",
       "      <td>978</td>\n",
       "      <td>4334280196</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>979</th>\n",
       "      <td>979</td>\n",
       "      <td>5637509252</td>\n",
       "      <td>20171222</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>10.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>54</td>\n",
       "      <td>54.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>980</th>\n",
       "      <td>980</td>\n",
       "      <td>4373601088</td>\n",
       "      <td>20180211</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>981</th>\n",
       "      <td>981</td>\n",
       "      <td>8400011508</td>\n",
       "      <td>20180207</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>7</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>982</th>\n",
       "      <td>982</td>\n",
       "      <td>4519652332</td>\n",
       "      <td>20180208</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>983</th>\n",
       "      <td>983</td>\n",
       "      <td>4519652236</td>\n",
       "      <td>20180129</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>16</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>984</th>\n",
       "      <td>984</td>\n",
       "      <td>4345512736</td>\n",
       "      <td>20180129</td>\n",
       "      <td>42.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>39.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>16</td>\n",
       "      <td>16.0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>985</th>\n",
       "      <td>985</td>\n",
       "      <td>7076787248</td>\n",
       "      <td>20180116</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.024800e+04</td>\n",
       "      <td>2.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>29</td>\n",
       "      <td>29.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.024800e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>986</th>\n",
       "      <td>986</td>\n",
       "      <td>4351130192</td>\n",
       "      <td>20180205</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.050000e+04</td>\n",
       "      <td>28.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>9</td>\n",
       "      <td>9.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.050000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>987</th>\n",
       "      <td>987</td>\n",
       "      <td>4362690608</td>\n",
       "      <td>20180210</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>988</th>\n",
       "      <td>988</td>\n",
       "      <td>7025003460</td>\n",
       "      <td>20171207</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>12.0</td>\n",
       "      <td>000</td>\n",
       "      <td>L</td>\n",
       "      <td>69</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>989</th>\n",
       "      <td>989</td>\n",
       "      <td>8142860464</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>990</th>\n",
       "      <td>990</td>\n",
       "      <td>8142860452</td>\n",
       "      <td>20171205</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>71</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>991</th>\n",
       "      <td>991</td>\n",
       "      <td>8142861488</td>\n",
       "      <td>20180204</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>10</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>992</th>\n",
       "      <td>992</td>\n",
       "      <td>6296435832</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>993</th>\n",
       "      <td>993</td>\n",
       "      <td>5907146384</td>\n",
       "      <td>20171218</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>58</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>994</th>\n",
       "      <td>994</td>\n",
       "      <td>4328664004</td>\n",
       "      <td>20180129</td>\n",
       "      <td>495.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>975.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>16</td>\n",
       "      <td>16.0</td>\n",
       "      <td>495.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>995</td>\n",
       "      <td>7025003388</td>\n",
       "      <td>20180125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>996</td>\n",
       "      <td>4328664896</td>\n",
       "      <td>20180210</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>997</td>\n",
       "      <td>8400005728</td>\n",
       "      <td>20171212</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>000</td>\n",
       "      <td>S</td>\n",
       "      <td>64</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>998</td>\n",
       "      <td>8142859248</td>\n",
       "      <td>20180129</td>\n",
       "      <td>20.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>6.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>16</td>\n",
       "      <td>16.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>999</td>\n",
       "      <td>4339898772</td>\n",
       "      <td>20180129</td>\n",
       "      <td>343.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>365.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>16</td>\n",
       "      <td>16.0</td>\n",
       "      <td>343.0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Unnamed: 0          ID  RECENT_DATE  TRANS_NUM  AVG_TRANS_AMT  \\\n",
       "0             0  4356689447     20180125        0.0   0.000000e+00   \n",
       "1             1  5985727751     20180125        0.0   0.000000e+00   \n",
       "2             2  8221439519     20171211        0.0   0.000000e+00   \n",
       "3             3  6255360379     20171226        0.0   0.000000e+00   \n",
       "4             4  5985726631     20171203        0.0   0.000000e+00   \n",
       "5             5  6255360403     20180109        0.0   0.000000e+00   \n",
       "6             6  8221440591     20171208        0.0   0.000000e+00   \n",
       "7             7  8221439459     20171208        0.0   0.000000e+00   \n",
       "8             8  4373540587     20171204        0.0   0.000000e+00   \n",
       "9             9  8221438327     20171122        0.0   0.000000e+00   \n",
       "10           10  8221438387     20171123        0.0   0.000000e+00   \n",
       "11           11  6255358175     20180206        0.0   0.000000e+00   \n",
       "12           12  4373539491     20180210        0.0   0.000000e+00   \n",
       "13           13  6255359199     20180126        0.0   0.000000e+00   \n",
       "14           14  5137506867     20180210        0.0   0.000000e+00   \n",
       "15           15  5137506855     20180129      207.0   0.000000e+00   \n",
       "16           16  6851792819     20171125        0.0   0.000000e+00   \n",
       "17           17  4362308131     20180131       24.0   5.302320e+05   \n",
       "18           18  7446429015     20180210        0.0   0.000000e+00   \n",
       "19           19  8491076627     20171128        1.0   5.000000e+04   \n",
       "20           20  4362703403     20171205        0.0   0.000000e+00   \n",
       "21           21  8491075519     20180201       20.0   4.966667e+04   \n",
       "22           22  8221435907     20171211        0.0   0.000000e+00   \n",
       "23           23  4334214987     20180130       48.0   4.800000e+04   \n",
       "24           24  4362301135     20171122        0.0   0.000000e+00   \n",
       "25           25  7103577783     20180115       27.0   5.400000e+04   \n",
       "26           26  6339286107     20180125        0.0   0.000000e+00   \n",
       "27           27  5985721899     20180120        0.0   0.000000e+00   \n",
       "28           28  4412856783     20180123        0.0   0.000000e+00   \n",
       "29           29  4356682487     20180202        0.0   0.000000e+00   \n",
       "..          ...         ...          ...        ...            ...   \n",
       "970         970  8142865064     20180125        0.0   0.000000e+00   \n",
       "971         971  4519660412     20180201        0.0   0.000000e+00   \n",
       "972         972  4339903456     20180125        0.0   0.000000e+00   \n",
       "973         973  4339903444     20180106      360.0   9.391169e+06   \n",
       "974         974  8142864968     20171116        0.0   0.000000e+00   \n",
       "975         975  4356749864     20180125        0.0   0.000000e+00   \n",
       "976         976  4519653416     20180210        0.0   0.000000e+00   \n",
       "977         977  9260714012     20180202        0.0   0.000000e+00   \n",
       "978         978  4334280196     20180125        0.0   0.000000e+00   \n",
       "979         979  5637509252     20171222        6.0   0.000000e+00   \n",
       "980         980  4373601088     20180211        0.0   0.000000e+00   \n",
       "981         981  8400011508     20180207        0.0   0.000000e+00   \n",
       "982         982  4519652332     20180208        0.0   0.000000e+00   \n",
       "983         983  4519652236     20180129        0.0   0.000000e+00   \n",
       "984         984  4345512736     20180129       42.0   0.000000e+00   \n",
       "985         985  7076787248     20180116        2.0   1.024800e+04   \n",
       "986         986  4351130192     20180205        7.0   1.050000e+04   \n",
       "987         987  4362690608     20180210        0.0   0.000000e+00   \n",
       "988         988  7025003460     20171207        0.0   0.000000e+00   \n",
       "989         989  8142860464     20180125        0.0   0.000000e+00   \n",
       "990         990  8142860452     20171205        0.0   0.000000e+00   \n",
       "991         991  8142861488     20180204        0.0   0.000000e+00   \n",
       "992         992  6296435832     20180125        0.0   0.000000e+00   \n",
       "993         993  5907146384     20171218        0.0   0.000000e+00   \n",
       "994         994  4328664004     20180129      495.0   0.000000e+00   \n",
       "995         995  7025003388     20180125        0.0   0.000000e+00   \n",
       "996         996  4328664896     20180210        0.0   0.000000e+00   \n",
       "997         997  8400005728     20171212        0.0   0.000000e+00   \n",
       "998         998  8142859248     20180129       20.0   0.000000e+00   \n",
       "999         999  4339898772     20180129      343.0   0.000000e+00   \n",
       "\n",
       "     TRANS_NUM_BEFORE  RFM USR_TYPE  DAYS     R      F             M  \n",
       "0                 0.0  000        S    20   NaN    NaN           NaN  \n",
       "1                 0.0  000        S    20   NaN    NaN           NaN  \n",
       "2                 0.0  000        S    65   NaN    NaN           NaN  \n",
       "3                 0.0  000        S    50   NaN    NaN           NaN  \n",
       "4                 0.0  000        S    73   NaN    NaN           NaN  \n",
       "5                 0.0  000        S    36   NaN    NaN           NaN  \n",
       "6                 0.0  000        S    68   NaN    NaN           NaN  \n",
       "7                 1.0  000        L    68   NaN    NaN           NaN  \n",
       "8                 0.0  000        S    72   NaN    NaN           NaN  \n",
       "9                 0.0  000        S    84   NaN    NaN           NaN  \n",
       "10                0.0  000        S    83   NaN    NaN           NaN  \n",
       "11                0.0  000        S     8   NaN    NaN           NaN  \n",
       "12                0.0  000        S     4   NaN    NaN           NaN  \n",
       "13                0.0  000        S    19   NaN    NaN           NaN  \n",
       "14                0.0  000        S     4   NaN    NaN           NaN  \n",
       "15              138.0                  16  16.0  207.0  0.000000e+00  \n",
       "16                0.0  000        S    81   NaN    NaN           NaN  \n",
       "17               84.0                  14  14.0   24.0  5.302320e+05  \n",
       "18                0.0  000        S     4   NaN    NaN           NaN  \n",
       "19                0.0                  78  78.0    1.0  5.000000e+04  \n",
       "20                0.0  000        S    71   NaN    NaN           NaN  \n",
       "21                0.0                  13  13.0   20.0  4.966667e+04  \n",
       "22                0.0  000        S    65   NaN    NaN           NaN  \n",
       "23               36.0                  15  15.0   48.0  4.800000e+04  \n",
       "24                0.0  000        S    84   NaN    NaN           NaN  \n",
       "25               63.0                  30  30.0   27.0  5.400000e+04  \n",
       "26                0.0  000        S    20   NaN    NaN           NaN  \n",
       "27                6.0  000        L    25   NaN    NaN           NaN  \n",
       "28                0.0  000        S    22   NaN    NaN           NaN  \n",
       "29               95.0  000        L    12   NaN    NaN           NaN  \n",
       "..                ...  ...      ...   ...   ...    ...           ...  \n",
       "970               0.0  000        S    20   NaN    NaN           NaN  \n",
       "971               0.0  000        S    13   NaN    NaN           NaN  \n",
       "972               0.0  000        S    20   NaN    NaN           NaN  \n",
       "973             640.0                  39  39.0  360.0  9.391169e+06  \n",
       "974               0.0  000        S    90   NaN    NaN           NaN  \n",
       "975               0.0  000        S    20   NaN    NaN           NaN  \n",
       "976               0.0  000        S     4   NaN    NaN           NaN  \n",
       "977               0.0  000        S    12   NaN    NaN           NaN  \n",
       "978               0.0  000        S    20   NaN    NaN           NaN  \n",
       "979              10.0                  54  54.0    6.0  0.000000e+00  \n",
       "980               0.0  000        S     3   NaN    NaN           NaN  \n",
       "981               0.0  000        S     7   NaN    NaN           NaN  \n",
       "982               0.0  000        S     6   NaN    NaN           NaN  \n",
       "983               0.0  000        S    16   NaN    NaN           NaN  \n",
       "984              39.0                  16  16.0   42.0  0.000000e+00  \n",
       "985               2.0                  29  29.0    2.0  1.024800e+04  \n",
       "986              28.0                   9   9.0    7.0  1.050000e+04  \n",
       "987               0.0  000        S     4   NaN    NaN           NaN  \n",
       "988              12.0  000        L    69   NaN    NaN           NaN  \n",
       "989               0.0  000        S    20   NaN    NaN           NaN  \n",
       "990               0.0  000        S    71   NaN    NaN           NaN  \n",
       "991               0.0  000        S    10   NaN    NaN           NaN  \n",
       "992               0.0  000        S    20   NaN    NaN           NaN  \n",
       "993               0.0  000        S    58   NaN    NaN           NaN  \n",
       "994             975.0                  16  16.0  495.0  0.000000e+00  \n",
       "995               0.0  000        S    20   NaN    NaN           NaN  \n",
       "996               0.0  000        S     4   NaN    NaN           NaN  \n",
       "997               0.0  000        S    64   NaN    NaN           NaN  \n",
       "998               6.0                  16  16.0   20.0  0.000000e+00  \n",
       "999             365.0                  16  16.0  343.0  0.000000e+00  \n",
       "\n",
       "[1000 rows x 12 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读入清洗后的用户投资数据。\n",
    "\n",
    "data = pd.read_csv('clean_data.csv', encoding = 'utf-8')\n",
    "\n",
    "# 标记流失用户和沉默用户。\n",
    "\n",
    "data['RFM'] = ''   \n",
    "data['USR_TYPE'] = ''\n",
    "\n",
    "# 流失用户 ( L ) 即为观测时间前曾有投资交易记录，但观测时间段内无投资记录的用户。\n",
    "\n",
    "data.ix[(data.TRANS_NUM_BEFORE > 0) & (data.TRANS_NUM == 0),['USR_TYPE']] = 'L'    \n",
    "data.ix[(data.TRANS_NUM_BEFORE > 0) & (data.TRANS_NUM == 0),['RFM']] = '000'\n",
    "\n",
    "# 沉默用户 ( S ) 即为注册开户至今，从未有过任何一笔投资交易记录的用户。\n",
    "\n",
    "data.ix[(data.TRANS_NUM_BEFORE == 0) & (data.TRANS_NUM == 0),['USR_TYPE']] = 'S'\n",
    "data.ix[(data. TRANS_NUM_BEFORE == 0) & (data.TRANS_NUM == 0),['RFM']] = '000'\n",
    "\n",
    "# 计算最近交易时间距离 2018 年 2 月 14 日的天数。\n",
    "\n",
    "data['DAYS'] =  [(pd.to_datetime('02/14/2018')- pd.to_datetime(str(t))).days \n",
    "                   for t in data['RECENT_DATE']] \n",
    "\n",
    "\n",
    "# 计算观测窗内有投资交易记录的用户的 R,F,M 指标。\n",
    "\n",
    "data['R'] = data[data.TRANS_NUM > 0]['DAYS']\n",
    "data['F'] = data[data.TRANS_NUM > 0]['TRANS_NUM']\n",
    "data['M'] = data[data.TRANS_NUM > 0]['AVG_TRANS_AMT']\n",
    "\n",
    "# 筛选有效 R,F,M 指标存入单独 dataframe，用于后续建立模型。\n",
    "\n",
    "data_RFM = data[['R','F','M']].dropna()\n",
    "\n",
    "data\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 22.3.4.\t数据标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>R</th>\n",
       "      <th>F</th>\n",
       "      <th>M</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>-0.117034</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.579722</td>\n",
       "      <td>-0.193889</td>\n",
       "      <td>-0.156358</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.554565</td>\n",
       "      <td>-0.203548</td>\n",
       "      <td>-0.186256</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.628696</td>\n",
       "      <td>-0.195569</td>\n",
       "      <td>-0.186276</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.530749</td>\n",
       "      <td>-0.183809</td>\n",
       "      <td>-0.186380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.203849</td>\n",
       "      <td>-0.192629</td>\n",
       "      <td>-0.186007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>-0.022540</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-1.069455</td>\n",
       "      <td>-0.093095</td>\n",
       "      <td>-0.159314</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.693582</td>\n",
       "      <td>-0.202708</td>\n",
       "      <td>-0.188024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.183314</td>\n",
       "      <td>0.123611</td>\n",
       "      <td>-0.185119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.350769</td>\n",
       "      <td>-0.193469</td>\n",
       "      <td>-0.120111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-0.579722</td>\n",
       "      <td>-0.202288</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>0.798507</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0.154876</td>\n",
       "      <td>-0.190529</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>-1.118428</td>\n",
       "      <td>-0.090575</td>\n",
       "      <td>-0.058631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>-1.118428</td>\n",
       "      <td>-0.203548</td>\n",
       "      <td>-0.189182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.987421</td>\n",
       "      <td>-0.188009</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>-0.187937</td>\n",
       "      <td>-0.203548</td>\n",
       "      <td>-0.189182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>2.652511</td>\n",
       "      <td>-0.167010</td>\n",
       "      <td>-0.183890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1.379207</td>\n",
       "      <td>-0.203548</td>\n",
       "      <td>-0.189337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>-1.020482</td>\n",
       "      <td>-0.166170</td>\n",
       "      <td>-0.159784</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>-0.971508</td>\n",
       "      <td>0.582221</td>\n",
       "      <td>-0.109233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>-0.334856</td>\n",
       "      <td>-0.202288</td>\n",
       "      <td>-0.178162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>1.183314</td>\n",
       "      <td>-0.203548</td>\n",
       "      <td>-0.187501</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>-0.971508</td>\n",
       "      <td>3.984003</td>\n",
       "      <td>2.900660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>0.105903</td>\n",
       "      <td>-0.198928</td>\n",
       "      <td>-0.180590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>0.007956</td>\n",
       "      <td>-0.203548</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>2.064833</td>\n",
       "      <td>-0.202288</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>-0.824589</td>\n",
       "      <td>0.331078</td>\n",
       "      <td>1.458932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>0.154876</td>\n",
       "      <td>-0.200188</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>327</th>\n",
       "      <td>2.260726</td>\n",
       "      <td>-0.203548</td>\n",
       "      <td>-0.189306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>328</th>\n",
       "      <td>-0.824589</td>\n",
       "      <td>-0.202288</td>\n",
       "      <td>-0.186835</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>329</th>\n",
       "      <td>-0.579722</td>\n",
       "      <td>-0.202708</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>330</th>\n",
       "      <td>-0.530749</td>\n",
       "      <td>-0.201448</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>331</th>\n",
       "      <td>2.407645</td>\n",
       "      <td>-0.189689</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>332</th>\n",
       "      <td>0.203849</td>\n",
       "      <td>-0.203128</td>\n",
       "      <td>-0.188123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>333</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>0.143770</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>334</th>\n",
       "      <td>1.428180</td>\n",
       "      <td>-0.200608</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>335</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>-0.202708</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>336</th>\n",
       "      <td>-0.824589</td>\n",
       "      <td>-0.202708</td>\n",
       "      <td>-0.188431</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>337</th>\n",
       "      <td>0.350769</td>\n",
       "      <td>-0.041439</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338</th>\n",
       "      <td>-1.118428</td>\n",
       "      <td>-0.173730</td>\n",
       "      <td>-0.121385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>339</th>\n",
       "      <td>-0.922535</td>\n",
       "      <td>-0.201448</td>\n",
       "      <td>-0.183868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>340</th>\n",
       "      <td>-1.069455</td>\n",
       "      <td>-0.056978</td>\n",
       "      <td>0.084298</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>341</th>\n",
       "      <td>0.301796</td>\n",
       "      <td>-0.027579</td>\n",
       "      <td>-0.186754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>342</th>\n",
       "      <td>-0.726642</td>\n",
       "      <td>-0.203548</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343</th>\n",
       "      <td>-0.824589</td>\n",
       "      <td>-0.203548</td>\n",
       "      <td>-0.189057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>344</th>\n",
       "      <td>-1.020482</td>\n",
       "      <td>-0.173730</td>\n",
       "      <td>-0.066034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>345</th>\n",
       "      <td>0.350769</td>\n",
       "      <td>-0.191369</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>346</th>\n",
       "      <td>0.399742</td>\n",
       "      <td>1.542280</td>\n",
       "      <td>7.576443</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>347</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>-0.095195</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>348</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>-0.197248</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>349</th>\n",
       "      <td>0.644609</td>\n",
       "      <td>-0.052778</td>\n",
       "      <td>0.395288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>350</th>\n",
       "      <td>1.379207</td>\n",
       "      <td>-0.201448</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>351</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>-0.186329</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>352</th>\n",
       "      <td>0.154876</td>\n",
       "      <td>-0.203128</td>\n",
       "      <td>-0.188730</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>353</th>\n",
       "      <td>-0.824589</td>\n",
       "      <td>-0.201028</td>\n",
       "      <td>-0.188715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>354</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>0.003919</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>355</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>-0.195569</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>356</th>\n",
       "      <td>-0.481776</td>\n",
       "      <td>-0.059917</td>\n",
       "      <td>-0.189368</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>357 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            R         F         M\n",
       "0   -0.481776 -0.117034 -0.189368\n",
       "1   -0.579722 -0.193889 -0.156358\n",
       "2    2.554565 -0.203548 -0.186256\n",
       "3   -0.628696 -0.195569 -0.186276\n",
       "4   -0.530749 -0.183809 -0.186380\n",
       "5    0.203849 -0.192629 -0.186007\n",
       "6   -0.481776 -0.022540 -0.189368\n",
       "7   -1.069455 -0.093095 -0.159314\n",
       "8    0.693582 -0.202708 -0.188024\n",
       "9    1.183314  0.123611 -0.185119\n",
       "10   0.350769 -0.193469 -0.120111\n",
       "11  -0.579722 -0.202288 -0.189368\n",
       "12  -0.481776  0.798507 -0.189368\n",
       "13   0.154876 -0.190529 -0.189368\n",
       "14  -1.118428 -0.090575 -0.058631\n",
       "15  -1.118428 -0.203548 -0.189182\n",
       "16   0.987421 -0.188009 -0.189368\n",
       "17  -0.187937 -0.203548 -0.189182\n",
       "18   2.652511 -0.167010 -0.183890\n",
       "19   1.379207 -0.203548 -0.189337\n",
       "20  -1.020482 -0.166170 -0.159784\n",
       "21  -0.971508  0.582221 -0.109233\n",
       "22  -0.334856 -0.202288 -0.178162\n",
       "23   1.183314 -0.203548 -0.187501\n",
       "24  -0.971508  3.984003  2.900660\n",
       "25   0.105903 -0.198928 -0.180590\n",
       "26   0.007956 -0.203548 -0.189368\n",
       "27   2.064833 -0.202288 -0.189368\n",
       "28  -0.824589  0.331078  1.458932\n",
       "29   0.154876 -0.200188 -0.189368\n",
       "..        ...       ...       ...\n",
       "327  2.260726 -0.203548 -0.189306\n",
       "328 -0.824589 -0.202288 -0.186835\n",
       "329 -0.579722 -0.202708 -0.189368\n",
       "330 -0.530749 -0.201448 -0.189368\n",
       "331  2.407645 -0.189689 -0.189368\n",
       "332  0.203849 -0.203128 -0.188123\n",
       "333 -0.481776  0.143770 -0.189368\n",
       "334  1.428180 -0.200608 -0.189368\n",
       "335 -0.481776 -0.202708 -0.189368\n",
       "336 -0.824589 -0.202708 -0.188431\n",
       "337  0.350769 -0.041439 -0.189368\n",
       "338 -1.118428 -0.173730 -0.121385\n",
       "339 -0.922535 -0.201448 -0.183868\n",
       "340 -1.069455 -0.056978  0.084298\n",
       "341  0.301796 -0.027579 -0.186754\n",
       "342 -0.726642 -0.203548 -0.189368\n",
       "343 -0.824589 -0.203548 -0.189057\n",
       "344 -1.020482 -0.173730 -0.066034\n",
       "345  0.350769 -0.191369 -0.189368\n",
       "346  0.399742  1.542280  7.576443\n",
       "347 -0.481776 -0.095195 -0.189368\n",
       "348 -0.481776 -0.197248 -0.189368\n",
       "349  0.644609 -0.052778  0.395288\n",
       "350  1.379207 -0.201448 -0.189368\n",
       "351 -0.481776 -0.186329 -0.189368\n",
       "352  0.154876 -0.203128 -0.188730\n",
       "353 -0.824589 -0.201028 -0.188715\n",
       "354 -0.481776  0.003919 -0.189368\n",
       "355 -0.481776 -0.195569 -0.189368\n",
       "356 -0.481776 -0.059917 -0.189368\n",
       "\n",
       "[357 rows x 3 columns]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "from pandas import DataFrame\n",
    "\n",
    "min_max_scaler = preprocessing.MinMaxScaler()\n",
    "data_minmax = pd.DataFrame(min_max_scaler.fit_transform(data_RFM),\n",
    "                           columns = ['R','F','M'])\n",
    "\n",
    "# 零均值标准化。\n",
    "data_scale = pd.DataFrame(preprocessing.scale(data_RFM),\n",
    "                          columns = ['R','F','M']) \n",
    "\n",
    "data_scale"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  22.3.5.\tK-means 聚类最优 K 值选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.cluster import KMeans\n",
    "\n",
    "# 设置模型参数，其中 n_cluster 为类别的个数，并开始训练。\n",
    "\n",
    "model = KMeans(n_clusters = 8)\n",
    "model.fit(data_scale)\n",
    "\n",
    "# 对每一类用户分别算出其 R、F、M 指标的相应平均值，高于平均值则赋值为 1，低于平均值则赋值为 0,R 指标则相反。\n",
    "\n",
    "avg_RFM = [np.mean(data_scale.R),np.mean(data_scale.F), np.mean(data_scale.M)] \n",
    "RFM = pd.DataFrame()\n",
    "RFM['R_type'] = ['1' if value[0] <= avg_RFM[0] else '0' \n",
    "                    for value in model.cluster_centers_]\n",
    "RFM['F_type'] = ['1' if value[1] >= avg_RFM[0] else '0' \n",
    "                    for value in model.cluster_centers_]\n",
    "RFM['M_type'] = ['1' if value[2] >= avg_RFM[0] else '0' \n",
    "                    for value in model.cluster_centers_]\n",
    "\n",
    "# 将 RFM 三个指标连接起来。\n",
    "\n",
    "RFM['RFM'] = RFM.R_type + RFM.F_type + RFM.M_type\n",
    "\n",
    "# 最后，得到每个用户通过聚类模型预测的类型结果。\n",
    "\n",
    "data.ix[data.TRANS_NUM > 0,['RFM']] = [RFM.ix[i,'RFM']  for i in model.labels_]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>R_type</th>\n",
       "      <th>F_type</th>\n",
       "      <th>M_type</th>\n",
       "      <th>RFM</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>011</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>111</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  R_type F_type M_type  RFM\n",
       "0      0      0      0  000\n",
       "1      1      0      0  100\n",
       "2      1      1      0  110\n",
       "3      0      1      1  011\n",
       "4      0      0      0  000\n",
       "5      1      1      1  111\n",
       "6      1      1      1  111\n",
       "7      1      1      1  111"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "RFM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      000\n",
       "1      000\n",
       "2      000\n",
       "3      000\n",
       "4      000\n",
       "5      000\n",
       "6      000\n",
       "7      000\n",
       "8      000\n",
       "9      000\n",
       "10     000\n",
       "11     000\n",
       "12     000\n",
       "13     000\n",
       "14     000\n",
       "15     100\n",
       "16     000\n",
       "17     100\n",
       "18     000\n",
       "19     000\n",
       "20     000\n",
       "21     100\n",
       "22     000\n",
       "23     100\n",
       "24     000\n",
       "25     000\n",
       "26     000\n",
       "27     000\n",
       "28     000\n",
       "29     000\n",
       "      ... \n",
       "970    000\n",
       "971    000\n",
       "972    000\n",
       "973    000\n",
       "974    000\n",
       "975    000\n",
       "976    000\n",
       "977    000\n",
       "978    000\n",
       "979    000\n",
       "980    000\n",
       "981    000\n",
       "982    000\n",
       "983    000\n",
       "984    100\n",
       "985    000\n",
       "986    100\n",
       "987    000\n",
       "988    000\n",
       "989    000\n",
       "990    000\n",
       "991    000\n",
       "992    000\n",
       "993    000\n",
       "994    100\n",
       "995    000\n",
       "996    000\n",
       "997    000\n",
       "998    100\n",
       "999    100\n",
       "Name: RFM, dtype: object"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.RFM"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
